﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <title>表单构建器</title>
    <link href="/CSS/bootstrap3.3.5/css/bootstrap.css" rel="stylesheet">
    <link href="/CSS/font-awesome-4.3.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="/CSS/Base/animate.css" rel="stylesheet">
    <!--<link href="/CSS/V3/summernote/summernote.css" rel="stylesheet">
    <link href="/CSS/V3/summernote/summernote-bs3.css" rel="stylesheet">-->
    <link href="/CSS/V3/style.css?v=2.2.0" rel="stylesheet">
    <link rel="stylesheet" href="/JqueryPlug/YanTip/YanTip.css" />

    <style>
        .droppable-active {
            background-color: #ffe !important;
        }

        .tools a {
            cursor: pointer;
            font-size: 80%;
        }

        .form-body .col-md-6,
        .form-body .col-md-12 {
            min-height: 400px;
        }

        .draggable {
            cursor: move;
        }
    </style>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content">
        <div class="row">
            <div class="col-sm-6">
                <div class="panel  panel-default float-e-margins">

                    <div class="page-body">
                        <div class="alert alert-info">
                            拖拽左侧的表单元素到右侧区域
                        </div>
                        <form role="form" class="form-horizontal m-t" style="margin:10px">
                            <div class="form-group draggable data-control" datatype="text" dataname="文本框">
                                <label class="col-sm-2 control-label">文本框</label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control" placeholder="">
                                </div>
                            </div>
                            <div class="form-group draggable data-control" datatype="textarea" dataname="多行文本框">
                                <label class="col-sm-2 control-label">多行文本框</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control szhl_UEEDIT"></textarea>
                                </div>
                            </div>

                            <div class="form-group draggable data-control" datatype="select" dataname="下拉列表" datalist="1,2">
                                <label class="col-sm-2 control-label">下拉列表</label>
                                <div class="col-sm-9">
                                    <select class="form-control">
                                        <option value="1">1</option>
                                        <option value="2">2</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group draggable data-control" datatype="check" dataname="复选框" datalist="1,2">
                                <label class="col-sm-2 control-label">复选框</label>
                                <div class="col-sm-9" id="conCheck">
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="1">1
                                    </label>
                                    <label class="checkbox-inline">
                                        <input type="checkbox" value="2">2
                                    </label>

                                </div>
                            </div>
                            <div class="form-group draggable" style="display:none">
                                <label class="col-sm-2 control-label">密码框：</label>
                                <div class="col-sm-9">
                                    <input type="password" class="form-control" name="password" placeholder="请输入密码">
                                </div>
                            </div>
                            <div class="form-group draggable" style="display:none">
                                <label class="col-sm-2 control-label">文件域：</label>
                                <div class="col-sm-9">
                                    <input type="file" name="" class="form-control ">
                                </div>
                            </div>
                            <div class="form-group draggable" style="display:none">
                                <label class="col-sm-2 control-label">纯文本：</label>
                                <div class="col-sm-9">
                                    <p class="form-control-static">这里是纯文字信息</p>
                                </div>
                            </div>
                            <div class="form-group draggable" style="display:none">
                                <label class="col-sm-2 control-label">
                                    单选框：
                                </label>
                                <div class="col-sm-9">
                                    <label class="radio-inline">
                                        <input type="radio" checked="" value="option1" id="optionsRadios1" name="optionsRadios">选项1
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" value="option2" id="optionsRadios2" name="optionsRadios">选项2
                                    </label>
                                </div>
                            </div>
                            <div class="form-group draggable data-control" datatype="table" dataname="表格" style="display:none">
                                <label class="col-sm-2 control-label">
                                    表格：
                                </label>
                                <div class="col-sm-9">
                                    <table class="table table-bordered table-hover table-responsive table-striped" contenteditable="true" >
                                        <thead>
                                            <tr>
                                                <th>列1</th><th>列2</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td>1</td><td>2</td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group draggable" style="display:none">
                                <div class="col-sm-12 col-sm-offset-3">
                                    <button class="btn btn-primary" type="submit">保存内容</button>
                                    <button class="btn btn-white" type="submit">取消</button>
                                </div>
                            </div>
                        </form>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>拖拽左侧表单元素到此区域</h5>
                        <div class="ibox-tools" style="display:none">
                            请选择显示的列数：
                            <select id="n-columns">
                                <option value="1">显示1列</option>
                                <option value="2" style="display:none">显示2列</option>
                            </select>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="row form-body form-horizontal m-t">
                            <div class="col-md-12 droppable sortable">
                            </div>
                            <div class="col-md-6 droppable sortable" style="display: none;">
                            </div>
                            <div class="col-md-6 droppable sortable" style="display: none;">
                            </div>
                        </div>
                        <button class="btn btn-warning" data-clipboard-text="" id="copy-to-clipboard">保存内容</button>
                    </div>
                </div>
            </div>

        </div>

        <div id="dvSet" style="display:none">
            <div class="form-group text">
                <label for="conName">名称</label>
                <input type="text" class="form-control" id="conName" placeholder="必须填写" value="文本框">
            </div>
            <!--<div class="form-group text">
                <label for="conMS">描述文字</label>
                <input type="text" class="form-control" id="conMS" placeholder="可选">
            </div>-->
            <div class="form-group select" style="display:none">
                <label for="conSEL">选项</label>
                <input type="text" class="form-control" id="conSEL" placeholder="用逗号隔开,例如1,2,3,4">
            </div>
        </div>
    </div>
    <!-- 全局js -->
    <script src="/Scripts/jquery-1.11.2.min.js"></script>
    <script src="/CSS/bootstrap3.3.5/js/bootstrap.js"></script>
    <script src="/Scripts/jquery-migrate-1.1.0.min.js"></script>

    <!-- 自定义js -->
    <!--<script src="/JqueryPlug/V3/content.js"></script>-->
    <!-- jQuery UI -->
    <script src="/JqueryPlug/V3/jquery-ui-1.10.4.min.js"></script>
    <!-- From Builder -->
    <script src="/JqueryPlug/V3/beautifyhtml.js"></script>
    <script type="text/javascript" src="/JqueryPlug/YanTip/YanTip.js"></script>
    <script src="/Scripts/SZHLCommon.js"></script>
    <script>
        $(document).ready(function () {
            setup_draggable();
            var CommonData;
            var PDID = ComFunJS.getQueryString("PDID")
            if (PDID) {
                $.getJSON('/API/FTAPI.ashx?Action=GETPROCESSBYID', { P1: PDID }, function (resultData) {
                    if (resultData.ErrorMsg == "") {
                        CommonData = resultData.Result;
                        if (CommonData.Tempcontent) {
                            $(".form-body .col-md-12").append(CommonData.Tempcontent);
                        }
                        $(".form-body .col-md-12").find(".form-group").each(function (i, group) {
                            $('<p class="tools col-sm-12 col-sm-offset-3">\
						<a class="edit-link">设置<a> | \
						<a class="remove-link">移除</a></p>').appendTo($(group)).addClass("ui-droppable").addClass("ui-sortable");
                        })
                    }
                })
            }
            $("#n-columns").on("change", function () {
                var v = $(this).val();
                if (v === "1") {
                    var $col = $('.form-body .col-md-12').toggle(true);
                    $('.form-body .col-md-6 .draggable').each(function (i, el) {
                        $(this).remove().appendTo($col);
                    })
                    $('.form-body .col-md-6').toggle(false);
                } else {
                    var $col = $('.form-body .col-md-6').toggle(true);
                    $(".form-body .col-md-12 .draggable").each(function (i, el) {
                        $(this).remove().appendTo(i % 2 ? $col[1] : $col[0]);
                    });
                    $('.form-body .col-md-12').toggle(false);
                }
            });

            $("#copy-to-clipboard").on("click", function () {
                var $copy = $(".form-body").clone();
                $copy.find(".tools").remove();
                $.each(["draggable", "droppable", "sortable", "dropped",
    "ui-sortable", "ui-draggable", "ui-droppable"], function (i, c) {
        $copy.find("." + c).removeClass(c).removeAttr("style");
    })
                $copy.find(".col-md-12").find("input").attr("dataid", "test");
                var array = [];
                var flag = true;
                $copy.find(".col-md-12 .form-group").each(function (i, dom) {
                    if (array.indexOf($(dom).attr("dataname")) < 0) {
                        array[i] = $(dom).attr("dataname");
                    } else {
                        parent.ComFunJS.winwarning("请删除重复项");
                        flag = false;
                    }
                })
                if (!flag) {
                    return;
                }
                var html = html_beautify($copy.find(".col-md-12").html());
                //需要添加将模版保存到数据库的逻辑
                if (PDID && CommonData) {
                    CommonData.Tempcontent = html;
                    $.post("/API/FTAPI.ashx", { Action: "ADDPROCESS", P1: JSON.stringify(CommonData), P2: PDID }, function (result) {
                        var jsonresult = $.parseJSON(result)
                        if ($.trim(jsonresult.ErrorMsg) != "") {
                            parent.ComFunJS.winwarning(jsonresult.ErrorMsg);
                        } else {
                            parent.ComFunJS.winsuccess("设置成功");
                        }
                    })
                }
                // console.debug(html)
                return false;
            })


        });

        var setup_draggable = function () {
            $(".draggable").draggable({
                appendTo: "body",
                helper: "clone"
            });
            $(".droppable").droppable({
                accept: ".draggable",
                helper: "clone",
                hoverClass: "droppable-active",
                drop: function (event, ui) {
                    $(".empty-form").remove();
                    var $orig = $(ui.draggable)
                    if (!$(ui.draggable).hasClass("dropped")) {
                        var $el = $orig
                            .clone()
                            .addClass("dropped")
                            .css({
                                "position": "static",
                                "left": null,
                                "right": null
                            })
                            .appendTo(this);

                        // update id
                        var id = $orig.find(":input").attr("id");

                        if (id) {
                            id = id.split("-").slice(0, -1).join("-") + "-" + (parseInt(id.split("-").slice(-1)[0]) + 1)

                            $orig.find(":input").attr("id", id);
                            $orig.find("label").attr("for", id);
                        }

                        // tools
                        $('<p class="tools col-sm-12 col-sm-offset-3">\
						<a class="edit-link">设置<a> | \
						<a class="remove-link">移除</a></p>').appendTo($el);
                    } else {
                        if ($(this)[0] != $orig.parent()[0]) {
                            var $el = $orig
                                .clone()
                                .css({
                                    "position": "static",
                                    "left": null,
                                    "right": null
                                })
                                .appendTo(this);
                            $orig.remove();
                        }
                    }
                }
            }).sortable();

        }




        //删除控件
        $(document).on("click", ".remove-link", function (ev) {
            $(this).parent().parent().remove();
        });

        //设置控件
        $(".edit-link").YanTip({
            content: $("#dvSet").html(),
            place: 'bottom',
            type: "click",
            title: "配置属性",
            isshowtitle: true,
            isshowfoot: true,
            showed: function ($tip, $dom) {//弹出设置页面事件
                var $datacontrol = $dom.parent().parent();
                $tip.find("#conName").val($datacontrol.attr("dataname"));
                var contype = $datacontrol.attr("datatype");

                if (contype == "select" || contype == "check") {
                    $tip.find(".select").show();
                    $tip.find("#conSEL").val($datacontrol.attr("datalist"));

                }
            },
            confirm: function ($tip, $dom) {//点击确定事件
                var $datacontrol = $dom.parent().parent();

                var labtext = $tip.find("#conName").val();
                var contype = $datacontrol.attr("datatype");
                //设置配置
                $datacontrol.find(".control-label").text(labtext);
                $datacontrol.attr("dataname", labtext);

                //设置配置
                //如果设置的是select
                if (contype == "select" || contype == "check") {
                    $datacontrol.attr("datalist", $tip.find("#conSEL").val());
                    if (contype == "select") {

                        $datacontrol.find(".form-control").children().remove();
                    } else {
                        $datacontrol.find(".col-sm-9").children().remove();
                    }
                    eval($tip.find("#conSEL").val().split(',')).forEach(function (val) {
                        if (contype == "select") {
                            $datacontrol.find(".form-control").append("<option value='"+val+"'>" + val + "</option>");
                        } else if (contype == "check") {
                            $datacontrol.find(".col-sm-9").append("<label class='checkbox-inline'> <input type='checkbox' value=" + val + ">" + val + " </label>");
                        }
                    })
                }

                $(".Yan_tip").remove();
            },
            cancel: function (tip) {
                alert(1)
            }
        });
    </script>
</body>
</html>